home *** CD-ROM | disk | FTP | other *** search
/ Acorn RISC PD-CD 1 / Acorn RISC PD-CD 1.iso / games / _kalaha / source / c / kalaha next >
Encoding:
Text File  |  1992-03-04  |  7.0 KB  |  353 lines

  1.  
  2. #include "kalaha.h"
  3.  
  4.  
  5. /******************  GLOBALS *********************************/
  6. char    *program_name = "Kalaha";
  7. char    *iconbar_menu_entries = ">Info,New Game,>Set Up,Quit";
  8. char    *NewGame_sub_entries = "Computer,User";
  9. char    version_string[40] = "1.00";
  10.  
  11.  
  12.  
  13. menu    iconbar_menu, NewGame_sub;
  14. wimp_w  main_window_handle;
  15. BOOL    main_window_open = FALSE;
  16.  
  17. /* Current board situation */
  18. char  bot[6] = {6, 6, 6, 6, 6, 6};
  19. char  top[6] = {6, 6, 6, 6, 6, 6};
  20. char  bottot = 1;
  21. char  toptot = 1;
  22. BOOL  botgo = TRUE;
  23. /* set up info */
  24. int   starterplayer = UserStarts;   
  25. int   level = 1;             /* level can also signify user v user */
  26. BOOL  soundon = TRUE;
  27. BOOL  automove = TRUE;
  28. /* for displaying move */
  29. int nofplops = 0;
  30. int plopsdone = 0;
  31. Plop pendingplops[74];
  32.  
  33.  
  34. /*******************  FUNCTIONS  ******************************/
  35. static BOOL create_window(char *name, wimp_w *handle);
  36.  
  37. static void open_window(wimp_openstr *o);
  38.  
  39. static void iconbar_click(wimp_i icon); 
  40.  
  41. static void setupdbox(void);
  42.  
  43. static void info_about_program(void);
  44.  
  45. static void iconbar_menu_proc(void *handle, char *hit);
  46.  
  47. static void main_window_events(wimp_eventstr *e, void *handle);
  48. /* the main events are dealt with by 
  49.       redraw_main_window() 
  50.       icon_hit(int i)
  51. */
  52. static void init_reinit(void);
  53.  
  54. static BOOL initialise(void);
  55.  
  56.  
  57.  
  58.  
  59. BOOL create_window(char *name, wimp_w *handle)
  60. {
  61. wimp_wind *window;    
  62.  
  63. window = template_syshandle(name);
  64. if (window == 0)
  65.   return FALSE;
  66.  
  67. return (wimpt_complain(wimp_create_wind(window, handle)) == 0);
  68. }
  69.  
  70.  
  71.  
  72. void open_window(wimp_openstr *o)
  73. {
  74. wimpt_noerr(wimp_open_wind(o));
  75. if ( !alarm_anypending((void *)0) )
  76.   alarm_set(alarm_timenow()+200, alarm_handler_proc, (void *)0 );
  77. }
  78.  
  79.  
  80.  
  81. void iconbar_click(wimp_i icon)
  82. {
  83. wimp_wstate  state;
  84. icon = icon;
  85.  
  86. if (!main_window_open)
  87.   {
  88.   wimpt_noerr(wimp_get_wind_state(main_window_handle, &state));
  89.   state.o.behind = -1;
  90.   open_window(&state.o);
  91.   main_window_open = TRUE;
  92.   }
  93. }
  94.  
  95.  
  96.  
  97. void setupdbox(void)
  98. {
  99. dbox  d;
  100. BOOL             filling = TRUE;
  101. int              i;
  102. wimp_w           dbhandle;
  103. int              i_hit[20];         /* safety net */
  104. wimp_which_block iblk;
  105. int              emask;
  106. wimp_redrawstr   r;
  107.  
  108.  
  109. d = dbox_new("SetUpdbox");
  110. if (d == NULL)
  111.   werr(TRUE, "Fatal internal error ( %d )", __LINE__);
  112.  
  113. /* fill in fields, set icon select states */
  114. dbhandle = dbox_syshandle(d);
  115.  
  116. for (i=SetUpdbox_L0; i<=SetUpdbox_UU; i++)
  117.     {
  118.     if (i==level+SetUpdbox_L0)
  119.        set_icon_select_state(dbhandle, i, TRUE);
  120.     else
  121.        set_icon_select_state(dbhandle, i, FALSE);
  122.     }
  123. dbox_setnumeric(d, SetUpdbox_Sound, soundon);
  124. dbox_setnumeric(d, SetUpdbox_Auto, automove);
  125.  
  126. /* ready for wimp_which_icon() */
  127. iblk.window = dbhandle;
  128. iblk.bit_mask = icon_ESGmask | wimp_ISELECTED;
  129.  
  130. /* open dbox */
  131. dbox_show(d);
  132. emask = event_getmask();
  133. event_setmask(0);      /* dbox seems to need this when alarm's on. */
  134. while (filling)
  135.   {
  136.   switch (dbox_fillin(d))
  137.      {
  138.      case 0:     /* hit on OK - gather all info */
  139.      iblk.bit_set = (2*icon_ESGlow) | wimp_ISELECTED;
  140.      wimpt_noerr(wimp_which_icon(&iblk, i_hit));
  141.      switch (*i_hit)
  142.          {
  143.          case SetUpdbox_L0:
  144.          case SetUpdbox_L1:
  145.          case SetUpdbox_L2:
  146.          case SetUpdbox_L3:
  147.          level = (*i_hit) - SetUpdbox_L0;
  148.          starterplayer = UserStarts;
  149.          menu_setflags(NewGame_sub, NewGame_sub_Comp, 0, 0);
  150.          break;            
  151.  
  152.          case SetUpdbox_UU:
  153.          level = (*i_hit) - SetUpdbox_L0;
  154.          starterplayer = UservUser;
  155.          menu_setflags(NewGame_sub, NewGame_sub_Comp, 0, 1);
  156.          break;
  157.          }
  158.      soundon = dbox_getnumeric(d, SetUpdbox_Sound);
  159.      automove = dbox_getnumeric(d, SetUpdbox_Auto);
  160.      filling = dbox_persist();
  161.      r.w = main_window_handle;       /* redraw arrow to reflect automove */
  162.      find_hollow_box(&(r.box), 14);  
  163.      wimp_force_redraw(&r);
  164.      break;
  165.  
  166.      case dbox_CLOSE:
  167.      filling = FALSE;
  168.      break;
  169.  
  170.      default:
  171.      break;
  172.      }
  173.    }
  174. dbox_dispose(&d);
  175. event_setmask(emask);
  176. }
  177.  
  178.  
  179.  
  180.  
  181. void info_about_program(void)
  182. {
  183. dbox  d;
  184.  
  185. if (d = dbox_new("info_dbox"), d != NULL)
  186.   {
  187.   dbox_setfield(d, info_dbox_version_field, version_string);
  188.   dbox_show(d);
  189.   dbox_fillin(d);
  190.   dbox_dispose(&d);
  191.   }
  192. }
  193.  
  194.  
  195.  
  196. void iconbar_menu_proc(void *handle, char *hit)
  197. {
  198. handle = handle;
  199.  
  200. switch (hit[0])
  201.   {
  202.   case iconbar_menu_Info:
  203.   info_about_program();
  204.   break;
  205.  
  206.   case iconbar_menu_NewGame:
  207.   switch (hit[1])
  208.     {
  209.     case 0:    /* No submenu hit.  ANSI C manual p343 says 
  210.                  hits terminated by -1 but really 0 */
  211.     if (starterplayer == ComputerStarts)
  212.         starterplayer = UserStarts;
  213.     else if (starterplayer == UserStarts)
  214.         starterplayer = ComputerStarts;
  215.     break;
  216.     
  217.     case NewGame_sub_Comp:
  218.     starterplayer = ComputerStarts;
  219.     break;
  220.  
  221.     case NewGame_sub_User:
  222.     starterplayer = UserStarts;
  223.     break;
  224.     }
  225.   init_reinit();
  226.   iconbar_click((wimp_i)0);
  227.   break;
  228.  
  229.   case iconbar_menu_SetUp:
  230.   setupdbox();
  231.   break;
  232.  
  233.   case iconbar_menu_Quit:
  234.   exit(0);
  235.   break;
  236.   }
  237. }
  238.  
  239.  
  240.  
  241. void main_window_events(wimp_eventstr *e, void *handle)
  242. {
  243. handle = handle;
  244.  
  245. switch (e->e)
  246.   {
  247.   case wimp_EREDRAW:
  248.   redraw_main_window(e->data.o.w);
  249.   break;
  250.  
  251.   case wimp_EOPEN:
  252.   open_window(&e->data.o);
  253.   break;
  254.  
  255.   case wimp_ECLOSE: 
  256.   alarm_removeall((void*)0);
  257.   wimpt_noerr(wimp_close_wind(e->data.o.w));
  258.   main_window_open = FALSE;
  259.   break;
  260.  
  261.   case wimp_EBUT:           
  262.   if (e->data.but.m.bbits == wimp_BLEFT)
  263.     icon_hit(e->data.but.m.i);
  264.   break;
  265.  
  266.   default:   
  267.   break;
  268.   }
  269. }
  270.                     
  271.  
  272.  
  273. void init_reinit(void)
  274. {
  275. int p;
  276.  
  277. srand( (unsigned int)alarm_timenow() );
  278.  
  279. alarm_removeall((void*)0);
  280. nofplops = plopsdone = 0;
  281.  
  282. for (p=0; p<6; p++)
  283.   { 
  284.   bot[p] = 6;
  285.   top[p] = 6;
  286.   toptot = bottot = 1;
  287.   }
  288. if (starterplayer==ComputerStarts)
  289.   botgo = FALSE;
  290. else 
  291.   botgo = TRUE;
  292.  
  293. for (p=0; p<13; p++)
  294.   {
  295.   if (p < 6)
  296.     write_iconnum(bot[p], p);
  297.   if (p >= 7)
  298.     write_iconnum(top[p-7], p);
  299.   set_icon_select_state(main_window_handle, p, FALSE);
  300.   }
  301. write_iconnum(bottot, 13);
  302. write_iconnum(toptot, 14);
  303.  
  304. wimpt_noerr(wimp_close_wind(main_window_handle));
  305. main_window_open = FALSE;
  306. }
  307.  
  308.  
  309. BOOL initialise(void)
  310. {
  311. wimpt_init(program_name);
  312. res_init(program_name);
  313. resspr_init();
  314. template_init();
  315. dbox_init();
  316. alarm_init();
  317.  
  318. if (!create_window("main_window", &main_window_handle))
  319.   return FALSE; 
  320. win_register_event_handler(main_window_handle, main_window_events, 0);
  321.  
  322. baricon("iconbaricon", (int)resspr_area(), iconbar_click);
  323.  
  324. iconbar_menu = menu_new(program_name, iconbar_menu_entries);
  325. if (iconbar_menu == NULL)
  326.   return FALSE; 
  327. if (!event_attachmenu(win_ICONBAR, iconbar_menu, iconbar_menu_proc, 0))
  328.   return FALSE;
  329. if (!event_attachmenu(main_window_handle, iconbar_menu, iconbar_menu_proc, 0))
  330.   return FALSE;
  331. NewGame_sub = menu_new("Who Starts", NewGame_sub_entries);
  332. if (NewGame_sub == NULL)
  333.   return FALSE; 
  334. menu_submenu(iconbar_menu, iconbar_menu_NewGame, NewGame_sub);
  335.  
  336. sprintf(version_string +strlen(version_string), " (%s)", __DATE__);
  337. return TRUE;
  338. }
  339.  
  340.  
  341.  
  342. int main(void)
  343. {
  344. if (initialise())
  345.   {
  346.   init_reinit();
  347.   while (TRUE)
  348.       event_process();
  349.   }
  350. exit(0);
  351. }
  352.  
  353.